package LeetCode;

import java.util.*;

//对角线遍历
public class LC_498 {
    public static int[] findDiagonalOrder(int[][] mat) {
        if (mat == null || mat.length == 0) {
            return new int[0];
        }

        int m = mat.length;  //行数
        int n = mat[0].length;  //列数
        List<Integer> list1 =new ArrayList<>();

        List<Integer> list2 = null;
        for (int i = 0; i < n; i++) {
            list2 =new ArrayList<>();
            int col = i;
            int row = 0;
            while(col>=0&&row<m){
                list2.add(mat[row++][col--]);
            }

            if(i%2!=0){
                list1.addAll(list2);
            }else{
                for (int j = list2.size()-1; j >=0 ; j--) {
                    list1.add(list2.get(j));
                }
            }
        }

        int r=n;

        for (int i = 1; i < m; i++) {
            list2 =new ArrayList<>();
            int col = n-1;
            int row = i;
            while(col>=0&&row<m){
                list2.add(mat[row++][col--]);
            }
            if(r%2!=0){
                list1.addAll(list2);
            }else{
                for (int j = list2.size()-1; j >=0 ; j--) {
                    list1.add(list2.get(j));
                }
            }
            r++;
        }



        return list1.stream().mapToInt(Integer::intValue).toArray();

    }

    public static void main(String[] args) {
        int[][] a = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        int[] s = findDiagonalOrder(a);
        for (int i:s
             ) {
            System.out.print(i);
        }

    }
}
